﻿using HWmanagementClient.HWManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HWmanagementClient.Codes;
using HWmanagementClient.Models;

namespace HWmanagementClient.Controllers
{
    public class HomeController : Controller
    {
        protected override void ExecuteCore()
        {
            try
            {
                ViewBag.Rank = HWmanagementClient.Codes.Site.GetAuthUserRank(Request, User);
            }
            catch
            {
                ViewBag.Rank = -1;
            }
            base.ExecuteCore();
        }

        public ActionResult Index()
        {
            UserModel loggedin = new UserModel();

            ViewBag.Message = "Házi feladat beadó rendszer";

            var a = Site.GetAuthUserData(Request, User);

            loggedin.Email = a.Item3;
            loggedin.Neptun = a.Item2;
            loggedin.Name = a.Item1;
            loggedin.Rank = Site.GetAuthUserRank(Request, User);

            return View(loggedin);
        }

        public ActionResult About()
        {
            UserModel loggedin = new UserModel();
            ViewBag.Message = "Névjegy";

            return View();
        }

        public ActionResult Admin()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Admin(AdminModel model)
        {
            try
            {
                ManagerClient proxy = new ManagerClient();
                proxy.AddNewUser(new UserModel()
                {
                    Name = model.User.Name,
                    Email = model.User.Email,
                    Neptun = model.User.Neptun.ToUpper(),
                    Password = model.User.Password.ToUpper(),
                    Rank = model.User.Rank
                });

                ViewBag.Created = true;

                return View();
            }
            catch
            {
                ViewBag.Created = false;
            }

            try
            {
                ManagerClient proxy = new ManagerClient();
                proxy.AddNewSubject(new SubjectModel()
                {
                    name = model.Subject.name,
                    neptun = model.Subject.neptun
                });

                ViewBag.Created = true;

                return View();
            }
            catch
            {
                ViewBag.Created = false;
            }

            try
            {
                ManagerClient proxy = new ManagerClient();
                proxy.AddCourse(model.Course.subjectid,model.Course.semester);

                ViewBag.Created = true;
            }
            catch
            {
                ViewBag.Created = false;
            }

            return View();
        }

        [HttpPost]
        public ActionResult GenerateDB()
        {
            bool isok = true;

            string[] vez = new[] { "Vinsler ", "Szentirmai-Kovács ", "Scinvert ", "Ágoston ", "Tóth ", "Dobó ", "Kátai ", "Jánosfalvi ", "Kováts ", "Csernus " };
            string[] ker = new[] { "Bogyó", "János", "Jácint", "Kázmér", "Tódor", "Piroska", "Eugénia", "Mirtill", "Noémi", "Katalin", };

            ManagerClient proxy = new ManagerClient();
            try
            {
                Random r = new Random(DateTime.Now.Millisecond + DateTime.Now.Second);
                for (int i = 0; i < 150; i++)
                {
                    string num = i < 10 ? "00" + i : i < 100 ? "0" + i : i.ToString();
                    proxy.AddNewUser(new UserModel()
                    {
                        Neptun = "STU" + num,
                        Name = string.Format("{0}{1}",vez[r.Next(10)],ker[r.Next(10)]),
                        Email = "hallg" + i + "BME@aut.teszt.bme.hu",
                        Rank = 2,
                        Password = "fc0098d88c5983520eae2836b753571ec674349f".ToUpper()
                    });
                }


                for (int i = 0; i < 150; i++)
                {
                    string num = i < 10 ? "00" + i : i < 100 ? "0" + i : i.ToString();
                    proxy.AddNewUser(new UserModel()
                    {
                        Neptun = "TEA" + num,
                        Name = string.Format("{0}{1} {2}", vez[r.Next(10)], ker[r.Next(10)], ker[r.Next(10)]),
                        Email = "okt" + i + "BME@aut.teszt.bme.hu",
                        Rank = 1,
                        Password = "ea0f3b9271eff45033bea3a6b1c36fa0000c3de7".ToUpper()
                    });
                }

                string[] ss = new[] { "Architekturális vezérlés ", "Gyakorlatorientált lépések ", "Programozás ", "Szélessáv ", ".NET fejelesztés ", "Integrált webes rendszerek ",
                    "Design és architektúra ", "Bevezetés az F# nyelvbe ", "Szabályozás elmélete ", "Rendszerminták ", };
                string[] lab = new[] { " előadás", " gyakorlat", " labor" };

                for (int i = 0; i < 150; i++)
                {
                    string num = i < 10 ? "00" + i : i < 100 ? "0" + i : i.ToString();
                    proxy.AddNewSubject(new SubjectModel()
                    {
                        name = ss[i % 10] + ((i % 4) + 1) + "." + lab[i % 3], 
                        neptun = "BMEVI"+num });
                    proxy.addTeacher("TEA" + num, "BMEVI" + num);
                    proxy.addTeacher("STU" + num, "BMEVI" + num);

                    proxy.AddCourse("BMEVI" + num, "2011/12/2");
                    proxy.AddCourse("BMEVI" + num, "2012/13/1");
                }

            }
            catch { isok = false; }

            return Json(isok);
        }
    }
}
